package com.anxin.act.task.complete.impl;

import com.alibaba.fastjson.JSONArray;
import com.anxin.act.task.complete.TaskComplete;
import com.anxin.act.task.entity.WfActHiTask;
import com.anxin.act.task.service.AddTaskService;
import com.anxin.act.task.service.WfActHiCommentService;
import com.anxin.act.task.service.WfActHiTaskService;
import com.anxin.act.task.service.WfActRuTaskService;
import com.anxin.act.task.vo.TaskVo;
import com.anxin.common.utils.sys.UserUtils;
import com.anxin.sys.system.entity.SysUser;
import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 转签提交
 * @author: liuxiangyu
 * @date: 2020/4/26 16:22
 */
@Service
public class EntrustCompleteImpl extends TaskComplete {


    @Autowired
    private WfActHiCommentService wfActHiCommentService;

    @Autowired
    private WfActHiTaskService wfActHiTaskService;

    @Autowired
    private WfActRuTaskService wfActRuTaskService;

    @Autowired
    private AddTaskService addTaskService;

    /**
     * 提交流程
     * @param taskVo
     */
    public String complete(TaskVo taskVo){
        // 进行转签
        JSONArray addUser = new JSONArray();
        addUser.add(taskVo.getEntrustUser());
        taskVo.setAddUser(addUser);

        // 保存当前任务的审批记录
        WfActHiTask wfActHiTask = wfActHiCommentService.getHiTask(taskVo);
        wfActHiTaskService.save(wfActHiTask);

        wfActHiCommentService.addComment(taskVo.getActId(),wfActHiTask.getId(), "转签:" + UserUtils.getNickNamesByUserArray(taskVo.getAddUser()));
        wfActHiCommentService.addAddTaskComment(taskVo,wfActHiTask.getId());

        // 为了让自由流转签后依然可以选择下一审批人 保留原taskType
        String taskType = taskVo.getViewRuTask().getType();

        if ("act_free_flow_approval".equals(taskVo.getProcessDefKey()) && ("actTask".equals(taskType) || "freeFlow".equals(taskType))){
            taskVo.setCompleteType("freeFlow");
        }



        wfActRuTaskService.addTask(taskVo);
        // 挂起流程
        addTaskService.hangTask(taskVo.getViewRuTask());

        return wfActHiTask.getId();
    }
}
